package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.http.callback.FollowCallBack;
import com.cgtong.android.mvp.contract.ProblemFollowedContract;
import com.cgtong.android.mvp.model.ProblemFollowedModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

/**
 * Created by chunc on 2017/10/26.
 */

public class ProblemFollowedPresenter implements ProblemFollowedContract.Presenter {


    private ProblemFollowedContract.Model mModle;
    private ProblemFollowedContract.View mView;

    public ProblemFollowedPresenter(ProblemFollowedContract.View view) {
        this.mView = view;
        mModle = new ProblemFollowedModel();
    }

    @Override
    public void httpProblemFollowedData(long maxId, String keyValue, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
//            mView.setRefreshing(true);
        }
        mModle.httpProblemFollowedData(maxId,  keyValue, new BaseCallBack<List<ProblemBean>>() {
            @Override
            public void onSuccess(List<ProblemBean> problemBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (problemBeen == null || problemBeen.size() == 0) {
                            mView.onNullData();
                            return;
                        } else if (problemBeen.size() < ConstantManager.PROBLEM_FOLLOWED_PAGE_SIZE_INT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpProblemFollowedDataSuccess(problemBeen, isRefresh);
                    } else {
                        mView.onHttpProblemFollowedDataSuccess(problemBeen, isRefresh);
                        if (problemBeen.size() < ConstantManager.PROBLEM_FOLLOWED_PAGE_SIZE_INT) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onError(e);
                }
            }
        });
    }

    @Override
    public void follow(long id, final int position) {
        mModle.follow(id, new FollowCallBack<ApiResult>() {
            @Override
            public void onFollowUserSuccess(long id, ApiResult apiResult) {
                if (mView != null){
                    mView.onFollowSuccess(id, position,apiResult);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null){
                    mView.onError(e);
                }
            }

        });
    }

    @Override
    public void onDestroy() {
        if (mView != null)
            mView = null;
    }
}
